/**
 * 模拟数据提供程序
 * 用于在API连接失败或开发阶段提供模拟数据
 */

// 导出所有模拟数据的集合
export const mockData = {};

// 告警管理模拟数据（提前定义，以便在dashboardOverview中引用）
export const alarmsMockData = mockData.alarms = {
  data: [    {
      alarmId: 'ALM-20250529-001',
      deviceId: 'AQM-123',
      deviceName: 'AQM-123-01',
      alarmType: 'PM2.5超标',
      alarmTime: '2025-05-29 09:30:00',
      alarmLevel: 'high',
      status: 'pending',
      alarmDetail: 'PM2.5值达到125μg/m³，超过标准值75μg/m³'
    },    {
      alarmId: 'ALM-20250529-002',
      deviceId: 'AQM-456',
      deviceName: 'AQM-456-02',
      alarmType: '电量不足',
      alarmTime: '2025-05-29 10:45:12',
      alarmLevel: 'medium',
      status: 'pending',
      alarmDetail: '设备电量低于15%，请及时充电'
    }
  ],
  headers: {
    'x-total-count': 2
  }
};

// 仪表板概览数据
export const dashboardOverview = mockData.dashboardOverview = {
  deviceCount: 42,
  onlineCount: 35,
  onlineRate: '83%',
  alarmCount: 8,
  alarmTrend: -5,
  errorCount: 3,
  errorTrend: -2,
  todayAlarmCount: 2,
  userCount: 15,  onlineDeviceCount: 35,  // 从告警模拟数据中获取待处理告警数量
  pendingAlarms: (() => {
    // 计算待处理的告警数量
    try {
      return alarmsMockData?.data?.filter(a => a.status === 'pending')?.length || 2;
    } catch (e) {
      return 2; // 默认值
    }
  })(),
  // 设备列表及电量信息  
  devices: [
    { id: 'AQM-123', name: 'AQM-123-01', battery: 85, status: 'online' },
    { id: 'AQM-456', name: 'AQM-456-02', battery: 15, status: 'warning' },
    { id: 'AQM-789', name: 'AQM-789-03', battery: 45, status: 'alarm' },
    { id: 'AQM-101', name: 'AQM-101-04', battery: 65, status: 'online' },
    { id: 'AQM-205', name: 'AQM-205-05', battery: 92, status: 'online' },
    { id: 'AQM-302', name: 'AQM-302-06', battery: 78, status: 'online' },
    { id: 'AQM-417', name: 'AQM-417-07', battery: 32, status: 'warning' },
    { id: 'AQM-523', name: 'AQM-523-08', battery: 54, status: 'online' },
    { id: 'AQM-610', name: 'AQM-610-09', battery: 61, status: 'online' },
    { id: 'AQM-725', name: 'AQM-725-10', battery: 25, status: 'warning' },
    { id: 'AQM-836', name: 'AQM-836-11', battery: 73, status: 'online' },
    { id: 'AQM-947', name: 'AQM-947-12', battery: 40, status: 'warning' },
    { id: 'AQM-058', name: 'AQM-058-13', battery: 88, status: 'online' },
    { id: 'AQM-169', name: 'AQM-169-14', battery: 95, status: 'online' },
    { id: 'AQM-270', name: 'AQM-270-15', battery: 12, status: 'alarm' },
    { id: 'AQM-381', name: 'AQM-381-16', battery: 67, status: 'online' },
    { id: 'AQM-492', name: 'AQM-492-17', battery: 50, status: 'online' },
    { id: 'AQM-503', name: 'AQM-503-18', battery: 35, status: 'warning' },
    { id: 'AQM-614', name: 'AQM-614-19', battery: 82, status: 'online' },
    { id: 'AQM-725', name: 'AQM-725-20', battery: 5, status: 'alarm' }
  ]
};

// 最近活动数据
export const recentActivities = mockData.recentActivities = [
  { timestamp: '2025-05-26 14:23:45', description: '设备 AQM-123-01 报告空气质量优' },
  { timestamp: '2025-05-26 12:15:32', description: '用户 admin 登录系统' },
  { timestamp: '2025-05-25 18:42:11', description: '设备 AQM-456-02 发送告警：PM2.5 超标' },
  { timestamp: '2025-05-25 09:30:00', description: '系统完成每日数据备份' },
  { timestamp: '2025-05-24 16:45:22', description: '设备 AQM-789-03 离线' },
  { timestamp: '2025-05-24 10:12:30', description: '用户 zhangsan 添加了设备 AQM-101-04' },
  { timestamp: '2025-05-23 15:30:22', description: '设备 AQM-302-06 启动自检程序' },
  { timestamp: '2025-05-23 09:15:10', description: '设备 AQM-417-07 电量低警告' }
];

// 设备使用状况数据
export const deviceUsage = mockData.deviceUsage = [
  { deviceName: 'AQM-123-01', userName: '张三', usageTime: 42, batteryLevel: 85, lastMaintenance: '2025-04-15', status: '正常', lastActive: '2025-05-26 14:23:45' },
  { deviceName: 'AQM-456-02', userName: '李四', usageTime: 36, batteryLevel: 15, lastMaintenance: '2025-03-20', status: '电量低', lastActive: '2025-05-26 10:45:12' },
  { deviceName: 'AQM-789-03', userName: '王五', usageTime: 28, batteryLevel: 45, lastMaintenance: '2025-05-10', status: '告警', lastActive: '2025-05-25 18:42:11' },
  { deviceName: 'AQM-101-04', userName: '赵六', usageTime: 48, batteryLevel: 65, lastMaintenance: '2025-04-28', status: '正常', lastActive: '2025-05-26 11:30:25' },
  { deviceName: 'AQM-205-05', userName: '钱七', usageTime: 15, batteryLevel: 92, lastMaintenance: '2025-05-18', status: '正常', lastActive: '2025-05-26 13:15:40' },
  { deviceName: 'AQM-302-06', userName: '郑八', usageTime: 33, batteryLevel: 78, lastMaintenance: '2025-05-05', status: '正常', lastActive: '2025-05-27 08:10:25' },
  { deviceName: 'AQM-417-07', userName: '孙九', usageTime: 22, batteryLevel: 32, lastMaintenance: '2025-04-20', status: '电量低', lastActive: '2025-05-27 09:05:18' },
  { deviceName: 'AQM-523-08', userName: '周十', usageTime: 39, batteryLevel: 54, lastMaintenance: '2025-05-12', status: '正常', lastActive: '2025-05-27 07:50:33' },
  { deviceName: 'AQM-610-09', userName: '吴一', usageTime: 27, batteryLevel: 61, lastMaintenance: '2025-05-15', status: '正常', lastActive: '2025-05-27 10:12:15' },
  { deviceName: 'AQM-725-10', userName: '郭二', usageTime: 19, batteryLevel: 25, lastMaintenance: '2025-03-28', status: '电量低', lastActive: '2025-05-27 11:30:22' },
  { deviceName: 'AQM-836-11', userName: '冯三', usageTime: 31, batteryLevel: 73, lastMaintenance: '2025-04-30', status: '正常', lastActive: '2025-05-26 16:45:08' },
  { deviceName: 'AQM-947-12', userName: '陈四', usageTime: 25, batteryLevel: 40, lastMaintenance: '2025-05-01', status: '电量低', lastActive: '2025-05-26 15:10:45' },
  { deviceName: 'AQM-058-13', userName: '褚五', usageTime: 45, batteryLevel: 88, lastMaintenance: '2025-05-19', status: '正常', lastActive: '2025-05-27 09:25:30' },
  { deviceName: 'AQM-169-14', userName: '卫六', usageTime: 50, batteryLevel: 95, lastMaintenance: '2025-05-20', status: '正常', lastActive: '2025-05-27 08:40:15' },
  { deviceName: 'AQM-270-15', userName: '蒋七', usageTime: 18, batteryLevel: 12, lastMaintenance: '2025-04-10', status: '告警', lastActive: '2025-05-26 14:05:33' },
  { deviceName: 'AQM-381-16', userName: '沈八', usageTime: 38, batteryLevel: 67, lastMaintenance: '2025-05-05', status: '正常', lastActive: '2025-05-27 07:15:20' },
  { deviceName: 'AQM-492-17', userName: '韩九', usageTime: 29, batteryLevel: 50, lastMaintenance: '2025-04-25', status: '正常', lastActive: '2025-05-26 17:30:45' },
  { deviceName: 'AQM-503-18', userName: '杨十', usageTime: 21, batteryLevel: 35, lastMaintenance: '2025-04-15', status: '电量低', lastActive: '2025-05-26 16:20:10' },
  { deviceName: 'AQM-614-19', userName: '朱一', usageTime: 42, batteryLevel: 82, lastMaintenance: '2025-05-10', status: '正常', lastActive: '2025-05-27 10:05:25' },
  { deviceName: 'AQM-725-20', userName: '秦二', usageTime: 15, batteryLevel: 5, lastMaintenance: '2025-03-15', status: '告警', lastActive: '2025-05-26 11:40:50' }
];

// 告警分布数据
export const alertDistribution = mockData.alertDistribution = [
  { name: 'PM2.5超标', value: 1 },
  { name: '电量不足', value: 1 },
  { name: '设备离线', value: 1 },
  { name: 'CO浓度异常', value: 1 },
  { name: '传感器故障', value: 1 },
  { name: '数据异常', value: 1 },
  { name: '通信异常', value: 1 },
  { name: 'TVOC超标', value: 1 },
  { name: '固件版本过低', value: 1 },
  { name: 'CO2超标', value: 1 }
];

// 告警管理模拟数据
export const alarmsMockData = mockData.alarms = {
  data: [    {
      alarmId: 'ALM-20250529-001',
      deviceId: 'AQM-123',
      deviceName: 'AQM-123-01',
      alarmType: 'PM2.5超标',
      alarmTime: '2025-05-29 09:30:00',
      alarmLevel: 'high',
      status: 'pending',
      alarmDetail: 'PM2.5值达到125μg/m³，超过标准值75μg/m³'
    },    {
      alarmId: 'ALM-20250529-002',
      deviceId: 'AQM-456',
      deviceName: 'AQM-456-02',
      alarmType: '电量不足',
      alarmTime: '2025-05-29 10:45:12',
      alarmLevel: 'medium',
      status: 'pending',
      alarmDetail: '设备电量低于15%，请及时充电'
    },    {
      alarmId: 'ALM-20250528-003',
      deviceId: 'AQM-789',
      deviceName: 'AQM-789-03',
      alarmType: '设备离线',
      alarmTime: '2025-05-28 18:42:11',
      alarmLevel: 'medium',
      status: 'processed',
      alarmDetail: '设备已超过6小时未上线，请检查设备状况',
      processRecord: '已联系维护人员检查'
    },    {
      alarmId: 'ALM-20250528-004',
      deviceId: 'AQM-101',
      deviceName: 'AQM-101-04',
      alarmType: 'CO浓度异常',
      alarmTime: '2025-05-28 15:20:30',
      alarmLevel: 'high',
      status: 'ignored',
      alarmDetail: 'CO浓度达到50ppm，超过警戒值30ppm'
    },    {
      alarmId: 'ALM-20250528-005',
      deviceId: 'AQM-205',
      deviceName: 'AQM-205-05',
      alarmType: '传感器故障',
      alarmTime: '2025-05-28 14:15:20',
      alarmLevel: 'low',
      status: 'pending',
      alarmDetail: '温湿度传感器读数异常，请检查传感器'
    },    {
      alarmId: 'ALM-20250527-006',
      deviceId: 'AQM-123',
      deviceName: 'AQM-123-01',
      alarmType: '数据异常',
      alarmTime: '2025-05-27 11:30:45',
      alarmLevel: 'low',
      status: 'processed',
      alarmDetail: '连续5分钟数据波动异常，可能需要校准',
      processRecord: '已重新校准设备参数'
    },    {
      alarmId: 'ALM-20250527-007',
      deviceId: 'AQM-456',
      deviceName: 'AQM-456-02',
      alarmType: '通信异常',
      alarmTime: '2025-05-27 15:40:22',
      alarmLevel: 'medium',
      status: 'pending',
      alarmDetail: '设备通信异常，数据包丢失率达到15%'
    },    {
      alarmId: 'ALM-20250526-008',
      deviceId: 'AQM-789',
      deviceName: 'AQM-789-03',
      alarmType: 'TVOC超标',
      alarmTime: '2025-05-26 08:15:30',
      alarmLevel: 'high',
      status: 'processed',
      alarmDetail: 'TVOC浓度达到0.8mg/m³，超过标准值0.6mg/m³',
      processRecord: '已开启通风系统，并通知工作人员'
    },    {
      alarmId: 'ALM-20250526-009',
      deviceId: 'AQM-101',
      deviceName: 'AQM-101-04',
      alarmType: '固件版本过低',
      alarmTime: '2025-05-26 09:20:15',
      alarmLevel: 'low',
      status: 'ignored',
      alarmDetail: '设备固件版本为1.2.3，最新版本为1.3.5，建议更新'
    },    {
      alarmId: 'ALM-20250525-010',
      deviceId: 'AQM-205',
      deviceName: 'AQM-205-05',
      alarmType: 'CO2超标',
      alarmTime: '2025-05-25 16:30:10',
      alarmLevel: 'medium',
      status: 'pending',
      alarmDetail: 'CO2浓度达到1500ppm，超过标准值1000ppm'
    }
  ],
  headers: {
    'x-total-count': 10
  }
};

// 设备列表数据
export const devices = mockData.deviceList = {
  devices: [
    { id: 1, deviceId: 'AQM-123', name: 'AQM-123-01', deviceName: 'AQM-123-01', type: '空气质量监测仪', status: 'online', deviceLocation: '区域一', location: '区域一', lastActive: '2025-05-27 07:30:45', battery: 85 },
    { id: 2, deviceId: 'AQM-456', name: 'AQM-456-02', deviceName: 'AQM-456-02', type: '空气质量监测仪', status: 'offline', deviceLocation: '区域二', location: '区域二', lastActive: '2025-05-26 18:12:33', battery: 15 },
    { id: 3, deviceId: 'AQM-789', name: 'AQM-789-03', deviceName: 'AQM-789-03', type: '空气质量监测仪', status: 'online', deviceLocation: '区域三', location: '区域三', lastActive: '2025-05-27 06:45:21', battery: 45 },
    { id: 4, deviceId: 'AQM-101', name: 'AQM-101-04', deviceName: 'AQM-101-04', type: '空气质量监测仪', status: 'online', deviceLocation: '区域四', location: '区域四', lastActive: '2025-05-27 07:15:10', battery: 65 },
    { id: 5, deviceId: 'AQM-205', name: 'AQM-205-05', deviceName: 'AQM-205-05', type: '空气质量监测仪', status: 'online', deviceLocation: '区域五', location: '区域五', lastActive: '2025-05-27 07:20:38', battery: 92 },
    { id: 6, deviceId: 'AQM-302', name: 'AQM-302-06', deviceName: 'AQM-302-06', type: '空气质量监测仪', status: 'online', deviceLocation: '区域六', location: '区域六', lastActive: '2025-05-27 08:10:25', battery: 78 },
    { id: 7, deviceId: 'AQM-417', name: 'AQM-417-07', deviceName: 'AQM-417-07', type: '空气质量监测仪', status: 'warning', deviceLocation: '区域七', location: '区域七', lastActive: '2025-05-27 09:05:18', battery: 32 },
    { id: 8, deviceId: 'AQM-523', name: 'AQM-523-08', deviceName: 'AQM-523-08', type: '空气质量监测仪', status: 'online', deviceLocation: '区域八', location: '区域八', lastActive: '2025-05-27 07:50:33', battery: 54 }
  ],
  total: 5
};

// 告警数据专门格式化的响应对象 (针对 dashboard/alerts API)
mockData.alertDistribution = {
  data: {
    recentActivities: mockData.recentActivities || [
      { timestamp: '2025-05-26 14:23:45', description: '设备 AQM-123-01 报告空气质量优' },
      { timestamp: '2025-05-26 12:15:32', description: '用户 admin 登录系统' },
      { timestamp: '2025-05-25 18:42:11', description: '设备 AQM-456-02 发送告警：PM2.5 超标' },
      { timestamp: '2025-05-25 09:30:00', description: '系统完成每日数据备份' },
      { timestamp: '2025-05-24 16:45:22', description: '设备 AQM-789-03 离线' }
    ],
    alertData: mockData.alertDistribution || [
      { name: 'PM2.5超标', value: 15 },
      { name: '电量不足', value: 12 },
      { name: '设备离线', value: 8 },
      { name: 'CO浓度异常', value: 5 },
      { name: '传感器故障', value: 3 }
    ],
    todayAlertCount: 7,
    pendingAlertCount: 4,
    criticalAlertCount: 2
  },
  success: true,
  message: '获取告警数据成功'
};

// 设备状态分布专门格式化的响应对象 (针对 dashboard/device-status API)
mockData.deviceStatus = {
  data: {
    deviceUsage: mockData.deviceUsage || [
      { deviceName: 'AQM-123-01', userName: '张三', usageTime: 42, batteryLevel: 85, lastMaintenance: '2025-04-15', status: '正常', lastActive: '2025-05-26 14:23:45' },
      { deviceName: 'AQM-456-02', userName: '李四', usageTime: 36, batteryLevel: 15, lastMaintenance: '2025-03-20', status: '电量低', lastActive: '2025-05-26 10:45:12' },
      { deviceName: 'AQM-789-03', userName: '王五', usageTime: 28, batteryLevel: 45, lastMaintenance: '2025-05-10', status: '告警', lastActive: '2025-05-25 18:42:11' },
      { deviceName: 'AQM-101-04', userName: '赵六', usageTime: 48, batteryLevel: 65, lastMaintenance: '2025-04-28', status: '正常', lastActive: '2025-05-26 11:30:25' },
      { deviceName: 'AQM-205-05', userName: '钱七', usageTime: 15, batteryLevel: 92, lastMaintenance: '2025-05-18', status: '正常', lastActive: '2025-05-26 13:15:40' }
    ],
    statusDistribution: {
      online: 35,
      offline: 7,
      warning: 8,
      alarm: 3,
      total: 53
    },
    batteryStatus: {
      critical: 4,
      low: 9,
      medium: 15,
      high: 25
    }
  },
  success: true,
  message: '获取设备状态分布成功'
};

// 今日数据 - 函数形式，可以根据设备ID参数生成特定数据
mockData.todayData = (params) => {
  const deviceId = params.deviceId || 'default';
  const today = new Date().toISOString().split('T')[0];
  const data = [];
  
  // 生成24小时的数据
  for (let hour = 0; hour < 24; hour++) {
    const timePoint = new Date();
    timePoint.setHours(hour, 0, 0, 0);
    
    // 基础数值，模拟不同设备有不同的数据范围
    const deviceFactor = deviceId.charCodeAt(deviceId.length - 1) % 10 / 10;
    
    data.push({
      time: timePoint.toISOString(),
      pm25: Math.round(20 + Math.random() * 40 + deviceFactor * 20),
      pm10: Math.round(40 + Math.random() * 60 + deviceFactor * 30),
      co2: Math.round(400 + Math.random() * 300 + deviceFactor * 150),
      tvoc: Math.round(200 + Math.random() * 400 + deviceFactor * 200),
      temperature: +(20 + Math.random() * 5 + deviceFactor * 2).toFixed(1),
      humidity: Math.round(40 + Math.random() * 30 + deviceFactor * 10)
    });
  }
  
  return {
    deviceId,
    date: today,
    data
  };
};

// 历史数据 - 函数形式，可以根据设备ID和天数参数生成特定数据
mockData.historyData = (params) => {
  const deviceId = params.deviceId || 'default';
  const days = parseInt(params.days || 7);
  const data = [];
  
  // 生成指定天数的历史数据
  for (let i = 0; i < days; i++) {
    const date = new Date();
    date.setDate(date.getDate() - i);
    const dateStr = date.toISOString().split('T')[0];
    
    // 基础数值，模拟不同设备有不同的数据范围
    const deviceFactor = deviceId.charCodeAt(deviceId.length - 1) % 10 / 10;
    
    data.push({
      date: dateStr,
      pm25: Math.round(20 + Math.random() * 40 + deviceFactor * 20),
      pm10: Math.round(40 + Math.random() * 60 + deviceFactor * 30),
      co2: Math.round(400 + Math.random() * 300 + deviceFactor * 150),
      tvoc: Math.round(200 + Math.random() * 400 + deviceFactor * 200),
      temperature: +(20 + Math.random() * 5 + deviceFactor * 2).toFixed(1),
      humidity: Math.round(40 + Math.random() * 30 + deviceFactor * 10),
      aqi: Math.round(50 + Math.random() * 50 + deviceFactor * 25)
    });
  }
  
  return {
    deviceId,
    period: `${days}天`,
    data
  };
};

// 仪表盘统计
mockData.dashboardStats = {
  dailyAverages: {
    pm25: 35,
    pm10: 65,
    co2: 580,
    tvoc: 320,
    temperature: 23.5,
    humidity: 45
  },
  weeklyTrend: {
    improving: ['pm25', 'co2'],
    worsening: ['tvoc'],
    stable: ['pm10', 'temperature', 'humidity']
  },
  recommendations: [
    '建议开窗通风15分钟',
    '当前空气质量良好',
    '周期性检查设备传感器',
    '部分设备电量低，建议充电'
  ],
  comparisonData: {
    thisWeek: {
      pm25: 35,
      pm10: 65,
      co2: 580,
      tvoc: 320
    },
    lastWeek: {
      pm25: 42,
      pm10: 63,
      co2: 620,
      tvoc: 280
    },
    changePercentage: {
      pm25: -16.7,
      pm10: 3.2,
      co2: -6.5,
      tvoc: 14.3
    }
  }
};

// 获取模拟数据或实际API数据
export const getDataOrMock = (apiData, mockData) => {
  if (apiData && (apiData.data || apiData.length > 0)) {
    return apiData.data || apiData;
  }
  return mockData;
};

// 判断是否为开发环境
export const isDev = process.env.NODE_ENV === 'development';

// 辅助函数：永久使用模拟数据
export const useMockData = true;

// 控制是否使用模拟数据 - 已默认启用并不再可配置
export const setUseMockData = (value) => { // eslint-disable-line no-unused-vars
  // 始终将useMockData设置为true
  localStorage.setItem('useMockData', 'true');
};

export const getUseMockData = () => {
  // 始终返回true，确保系统使用模拟数据
  localStorage.setItem('useMockData', 'true');
  return true;
};

// 用户模拟数据
export const usersMockData = mockData.userList = {
  admin: {
    id: 'admin',
    userId: 'admin',
    username: 'admin',
    name: '系统管理员',
    email: 'admin@airquality.com',
    phone: '13800138000',
    role: 'admin',
    status: 'active',
    lastLogin: '2025-05-29 08:30:45',
    createTime: '2025-01-01 00:00:00'
  },
  user1: {
    id: 'user1',
    userId: 'user1',
    username: 'zhangsan',
    name: '张三',
    email: 'zhangsan@example.com',
    phone: '13911112222',
    role: 'user',
    status: 'active',
    lastLogin: '2025-05-28 15:20:30',
    createTime: '2025-01-15 10:00:00'
  },
  user2: {
    id: 'user2',
    userId: 'user2',
    username: 'lisi',
    name: '李四',
    email: 'lisi@example.com',
    phone: '13933334444',
    role: 'user',
    status: 'active',
    lastLogin: '2025-05-29 09:10:25',
    createTime: '2025-02-20 14:30:00'
  }
};

// 系统设置模拟数据 - 默认设置
export const systemSettingsMockData = mockData.systemSettings = {
  autoRefresh: true,
  refreshInterval: 30,
  theme: 'light',
  notifications: ['email', 'push'],
  language: 'zh-CN',
  dataSyncEnabled: true,
  syncInterval: 60,
  dataRetention: 90, // 保留数据天数
  alarmThresholds: {
    pm25: 75,
    pm10: 150,
    co2: 1000,
    tvoc: 0.6,
    temperature: {
      min: 18,
      max: 28
    },
    humidity: {
      min: 30,
      max: 60
    }
  }
};

// 更新告警分布数据，根据当前告警数据
export const updateAlertDistribution = () => {
  // 创建告警类型计数映射
  const alarmTypeCount = {};
  
  // 遍历所有告警并计数
  alarmsMockData.data.forEach(alarm => {
    if (alarmTypeCount[alarm.alarmType]) {
      alarmTypeCount[alarm.alarmType]++;
    } else {
      alarmTypeCount[alarm.alarmType] = 1;
    }
  });
  
  // 清空现有分布数据
  alertDistribution.length = 0;
  
  // 添加新的分布数据
  Object.keys(alarmTypeCount).forEach(alarmType => {
    alertDistribution.push({
      name: alarmType,
      value: alarmTypeCount[alarmType]
    });
  });
  
  return alertDistribution;
};
